Component({
  properties: {
    // 标题
    title: {
      type: String,
      value: '标题'
    },
    // 副标题
    subtitle: {
      type: String,
      value: ''
    },
    // 标题样式类
    titleClass: {
      type: String,
      value: ''
    },
    // 是否显示返回按钮
    showBack: {
      type: Boolean,
      value: true
    },
    // 是否显示搜索按钮
    showSearch: {
      type: Boolean,
      value: false
    },
    // 是否显示分享按钮
    showShare: {
      type: Boolean,
      value: false
    },
    // 是否显示更多按钮
    showMore: {
      type: Boolean,
      value: false
    },
    // 是否透明背景
    transparent: {
      type: Boolean,
      value: false
    },
    // 是否固定定位
    fixed: {
      type: Boolean,
      value: false
    },
    // 是否显示进度条
    showProgress: {
      type: Boolean,
      value: false
    },
    // 进度百分比
    progress: {
      type: Number,
      value: 0
    },
    // 主题
    theme: {
      type: String,
      value: 'dark' // dark, light
    },
    // 是否显示主页按钮
    showHome: {
      type: Boolean,
      value: false
    }
  },

  data: {
    scrolled: false,
    statusBarHeight: 0
  },

  lifetimes: {
    attached() {
      // 监听页面滚动
      this.setupScrollListener();
      // 获取状态栏高度
      const systemInfo = wx.getSystemInfoSync();
      this.setData({
        statusBarHeight: systemInfo.statusBarHeight
      });
    },
    
    detached() {
      // 清理滚动监听
      this.cleanupScrollListener();
    }
  },

  methods: {
    // 返回按钮点击
    onBack() {
      this.triggerEvent('back');
      // 默认行为：返回上一页
      if (getCurrentPages().length > 1) {
        wx.navigateBack();
      } else {
        wx.switchTab({
          url: '/pages/index/index'
        });
      }
    },

    // 搜索按钮点击
    onSearch() {
      this.triggerEvent('search');
    },

    // 分享按钮点击
    onShare() {
      this.triggerEvent('share');
    },

    // 更多按钮点击
    onMore() {
      this.triggerEvent('more');
    },

    // 设置滚动监听
    setupScrollListener() {
      if (!this.data.fixed) return;
      
      this.scrollListener = wx.createIntersectionObserver(this)
        .relativeToViewport({ top: 0 })
        .observe('.navbar', (res) => {
          const scrolled = res.boundingClientRect.top < 0;
          if (scrolled !== this.data.scrolled) {
            this.setData({ scrolled });
            this.triggerEvent('scroll', { scrolled });
          }
        });
    },

    // 清理滚动监听
    cleanupScrollListener() {
      if (this.scrollListener) {
        this.scrollListener.disconnect();
      }
    },

    // 设置进度
    setProgress(progress) {
      this.setData({ progress: Math.max(0, Math.min(100, progress)) });
    },

    // 显示加载状态
    showLoading() {
      this.setData({ 
        showProgress: true,
        progress: 0
      });
      
      // 模拟加载进度
      let progress = 0;
      const timer = setInterval(() => {
        progress += Math.random() * 30;
        if (progress >= 100) {
          progress = 100;
          clearInterval(timer);
          setTimeout(() => {
            this.hideLoading();
          }, 500);
        }
        this.setProgress(progress);
      }, 200);
    },

    // 隐藏加载状态
    hideLoading() {
      this.setData({ 
        showProgress: false,
        progress: 0
      });
    },

    // 显示成功状态
    showSuccess(message = '操作成功') {
      const originalClass = this.data.titleClass;
      this.setData({ 
        titleClass: 'success',
        title: message
      });
      
      setTimeout(() => {
        this.setData({ 
          titleClass: originalClass
        });
      }, 2000);
    },

    // 显示错误状态
    showError(message = '操作失败') {
      const originalClass = this.data.titleClass;
      this.setData({ 
        titleClass: 'error',
        title: message
      });
      
      setTimeout(() => {
        this.setData({ 
          titleClass: originalClass
        });
      }, 2000);
    },

    // 返回上一页
    navigateBack() {
      wx.navigateBack({
        delta: 1,
        fail: () => {
          wx.switchTab({
            url: '/pages/index/index'
          });
        }
      });
    },
    
    // 返回首页
    navigateHome() {
      wx.switchTab({
        url: '/pages/index/index'
      });
    }
  }
}); 